package com.lyc.yuki.module.system.service.impl;

import cn.hutool.core.util.DesensitizedUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyc.yuki.module.system.domain.po.UserPO;
import com.lyc.yuki.module.system.domain.vo.UserVO;
import com.lyc.yuki.module.system.mapper.UserMapper;
import com.lyc.yuki.module.system.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: lyc
 * @Date: 2025/7/8 21:39
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserPO> implements UserService {

    @Override
    public IPage<UserVO> selectUserPage(Integer current, Integer size) {
        Page<UserVO> page = new Page<>(current, size);
        IPage<UserVO> userVOPage = baseMapper.selectUserPage(page, null);

        List<UserVO> originalRecords = userVOPage.getRecords();
        List<UserVO> newRecords = new ArrayList<>(originalRecords.size());
        for (UserVO user : originalRecords) {
            UserVO userVO = new UserVO();
            BeanUtils.copyProperties(user, userVO);
            userVO.setPhone(DesensitizedUtil.mobilePhone(user.getPhone()));
            userVO.setEmail(DesensitizedUtil.email(user.getEmail()));
            newRecords.add(userVO);
        }
        userVOPage.setRecords(newRecords);

        return userVOPage;
    }

    @Override
    public List<UserVO> getUserListByUserName(String username) {
        return baseMapper.getUserListByUserName(username);
    }
}
